import { apiClient } from './client';
import { API_ENDPOINTS } from './config';
import type {
  MemberGradeResponse,
  MemberInfo,
  UpdatePasswordRequest,
  UpdateEmailRequest,
  SendVerifyCodeRequest,
  RetrievePasswordRequest,
  RetrievePasswordResponse,
  GoogleAuthenticatorResponse,
  GoogleAuthenticatorSubmitRequest,
  UnbindGoogleAuthenticatorRequest
} from '../../types/member';
import type { PaginationParams } from '../../types/api';

export const memberApi = {
  // 获取会员等级列表
  getGradeList: (params: PaginationParams) =>
    apiClient.get<MemberGradeResponse>(API_ENDPOINTS.GRADE_LIST, { params }),

  // 获取会员信息
  getMemberInfo: () =>
    apiClient.get<MemberInfo>(API_ENDPOINTS.MEMBER_INFO),

  // 修改登录密码
  updatePassword: (data: UpdatePasswordRequest) =>
    apiClient.post(API_ENDPOINTS.UPDATE_PASSWORD, data),

  // 更新邮箱
  updateEmail: (data: UpdateEmailRequest) =>
    apiClient.post('/api/v1/member/update-email', data),

  // 发送验证码
  sendVerifyCode: (data: SendVerifyCodeRequest) => {
    delete data.scene
    return apiClient.post('/api/v1/login/send-email', data)
  },

  // 获取谷歌验证器绑定信息
  getGoogleAuthenticator: () =>
    apiClient.get<GoogleAuthenticatorResponse>('/api/v1/member/bind-google-authenticator'),

  // 提交谷歌验证器绑定
  submitGoogleAuthenticator: (data: GoogleAuthenticatorSubmitRequest) =>
    apiClient.post<MemberInfo>('/api/v1/member/google-authenticator-submit', data),

  // 解绑谷歌验证器
  unbindGoogleAuthenticator: (data: UnbindGoogleAuthenticatorRequest) =>
    apiClient.post<MemberInfo>('/api/v1/member/unbind-google-authenticator', data),

  /**
   * 找回密码
   */
  async retrievePassword(data: RetrievePasswordRequest): Promise<RetrievePasswordResponse> {
    return apiClient.post('/api/v1/login/retrieve-password', data);
  },
};
